//-----------------------------------------------------------------------
// <copyright file="MemberDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class MemberDAOBase
    {
        #region Common methods
        public virtual Member CreateMemberFromReader(IDataReader reader)
        {
            Member item = new Member();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MemberID"))) item.MemberID = (long)reader["MemberID"];
                if (!reader.IsDBNull(reader.GetOrdinal("NameDisplay"))) item.NameDisplay = (string)reader["NameDisplay"];
                if (!reader.IsDBNull(reader.GetOrdinal("Password"))) item.Password = (string)reader["Password"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("FullName"))) item.FullName = (string)reader["FullName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Sex"))) item.Sex = (int)reader["Sex"];
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) item.Address = (string)reader["Address"];
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) item.Phone = (string)reader["Phone"];
                if (!reader.IsDBNull(reader.GetOrdinal("Fax"))) item.Fax = (string)reader["Fax"];
                if (!reader.IsDBNull(reader.GetOrdinal("ForgotCode"))) item.ForgotCode = (string)reader["ForgotCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("ActivateCode"))) item.ActivateCode = (string)reader["ActivateCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (bool)reader["Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("GoldMember"))) item.GoldMember = (bool)reader["GoldMember"];
                if (!reader.IsDBNull(reader.GetOrdinal("MembershipFeeID"))) item.MembershipFeeID = (int)reader["MembershipFeeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartDateGoldmember"))) item.StartDateGoldmember = (DateTime)reader["StartDateGoldmember"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDateGoldmember"))) item.EndDateGoldmember = (DateTime)reader["EndDateGoldmember"];
                if (!reader.IsDBNull(reader.GetOrdinal("Other"))) item.Other = (string)reader["Other"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMemberFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateMember methods
            
        public virtual void CreateMember(Member member)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberInsert");
                
                database.AddInParameter(dbCommand, "@NameDisplay", DbType.String, member.NameDisplay);
                database.AddInParameter(dbCommand, "@Password", DbType.String, member.Password);
                database.AddInParameter(dbCommand, "@Email", DbType.String, member.Email);
                database.AddInParameter(dbCommand, "@FullName", DbType.String, member.FullName);
                database.AddInParameter(dbCommand, "@Sex", DbType.Int32, member.Sex);
                database.AddInParameter(dbCommand, "@Address", DbType.String, member.Address);
                database.AddInParameter(dbCommand, "@Phone", DbType.String, member.Phone);
                database.AddInParameter(dbCommand, "@Fax", DbType.String, member.Fax);
                database.AddInParameter(dbCommand, "@ForgotCode", DbType.String, member.ForgotCode);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, member.ActivateCode);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, member.Status);
                database.AddInParameter(dbCommand, "@GoldMember", DbType.Boolean, member.GoldMember);
                database.AddInParameter(dbCommand, "@MembershipFeeID", DbType.Int32, member.MembershipFeeID);
                database.AddInParameter(dbCommand, "@StartDateGoldmember", DbType.DateTime, member.StartDateGoldmember);
                database.AddInParameter(dbCommand, "@EndDateGoldmember", DbType.DateTime, member.EndDateGoldmember);
                database.AddInParameter(dbCommand, "@Other", DbType.String, member.Other);
                database.AddOutParameter(dbCommand, "@MemberID", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                member.MemberID = (long)database.GetParameterValue(dbCommand, "@MemberID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMemberException, ex);
            }
        }

        #endregion

        #region UpdateMember methods
        
        public virtual void UpdateMember(Member member)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberUpdate");            
                
                database.AddInParameter(dbCommand, "@MemberID", DbType.Int64, member.MemberID);
                database.AddInParameter(dbCommand, "@NameDisplay", DbType.String, member.NameDisplay);
                database.AddInParameter(dbCommand, "@Password", DbType.String, member.Password);
                database.AddInParameter(dbCommand, "@Email", DbType.String, member.Email);
                database.AddInParameter(dbCommand, "@FullName", DbType.String, member.FullName);
                database.AddInParameter(dbCommand, "@Sex", DbType.Int32, member.Sex);
                database.AddInParameter(dbCommand, "@Address", DbType.String, member.Address);
                database.AddInParameter(dbCommand, "@Phone", DbType.String, member.Phone);
                database.AddInParameter(dbCommand, "@Fax", DbType.String, member.Fax);
                database.AddInParameter(dbCommand, "@ForgotCode", DbType.String, member.ForgotCode);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, member.ActivateCode);
                database.AddInParameter(dbCommand, "@Status", DbType.Boolean, member.Status);
                database.AddInParameter(dbCommand, "@GoldMember", DbType.Boolean, member.GoldMember);
                database.AddInParameter(dbCommand, "@MembershipFeeID", DbType.Int32, member.MembershipFeeID);
                database.AddInParameter(dbCommand, "@StartDateGoldmember", DbType.DateTime, member.StartDateGoldmember);
                database.AddInParameter(dbCommand, "@EndDateGoldmember", DbType.DateTime, member.EndDateGoldmember);
                database.AddInParameter(dbCommand, "@Other", DbType.String, member.Other);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateMemberException, ex);
            }
        }
        
        #endregion

        #region DeleteMember methods
        public virtual void DeleteMember(long memberID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberDelete");
                
                database.AddInParameter(dbCommand, "@MemberID", DbType.Int64, memberID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteMemberException, ex);
            }
        }

        #endregion

        #region GetMember methods
        
        public virtual Member GetMember(long memberID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberGet");
                
                database.AddInParameter(dbCommand, "@MemberID", DbType.Int64, memberID);
                
                Member member =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        member = CreateMemberFromReader(reader);
                        reader.Close();
                    }
                }
                return member;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMemberException, ex);
            }
        }
        
        #endregion

        #region GetMemberList methods
        public virtual MemberCollection GetMemberList(MemberColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MemberCollection memberCollection = new MemberCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Member member = CreateMemberFromReader(reader);
                        memberCollection.Add(member);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return memberCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMemberListException, ex);
            }
        }
        
        public virtual MemberCollection GetMemberList(MemberColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetMemberList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetMemberList methods
        public virtual MemberCollection GetMemberListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMemberGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MemberCollection memberCollection = new MemberCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Member member = CreateMemberFromReader(reader);
                        memberCollection.Add(member);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return memberCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMemberListException, ex);
            }
        }
        
        public virtual MemberCollection GetMemberListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetMemberListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}